package ru.usu.gv.gui.analysis;

import ru.usu.gv.core.GraphCanvas;
import ru.usu.gv.graph.Graph;
import ru.usu.gv.uicomponents.UIGraph;
import ru.usu.gv.utils.GraphAlgorithmHelper;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

/**
 * @author spupyrev
 * 31.10.2008
 */
public class CommonStatisticsAnalyzer implements IGraphAnalyzer
{
	private JTextField vertexCountField;
	private JTextField edgeCountField;
	private JTextField ccCountField;

	public CommonStatisticsAnalyzer()
	{
		JPanel panel = AnalysisPanel.getInstance().getCommonStatisticsPanel();
		panel.setBorder(new javax.swing.border.TitledBorder("Статистика графа"));
		panel.setLayout(new GridLayout(3, 2));

		panel.add(new JLabel("Количество вершин"));
		vertexCountField = createTextField();
		panel.add(vertexCountField);

		panel.add(new JLabel("Количество ребер"));
		edgeCountField = createTextField();
		panel.add(edgeCountField);

		panel.add(new JLabel("Компонент связности"));
		ccCountField = createTextField();
		panel.add(ccCountField);
	}

	private JTextField createTextField()
	{
		JTextField field = new JTextField();
		field.setEnabled(false);
		field.setDisabledTextColor(Color.BLACK);
		field.setPreferredSize(new Dimension(30, 20));
		field.setHorizontalAlignment(JTextField.CENTER);
		return field;
	}

	public void doAnalyse()
	{
		UIGraph uiGraph = GraphCanvas.getInstance().getCurrentGraph();
		Graph graph = uiGraph.getGraph();

		vertexCountField.setText("" + graph.getSize());
		edgeCountField.setText("" + graph.getEdgeSize());
		ccCountField.setText("" + GraphAlgorithmHelper.getConnectedComponentsCount(graph));
	}

}
